Skip to content

Conversation

AstroAir
Copy link

@AstroAir AstroAir commented Oct 4, 2025

This pull request adds first-class support for Zhipu AI (智谱AI) models to Pydantic AI, including documentation, provider implementation, model profiles, and tests. Users can now use Zhipu's OpenAI-compatible API seamlessly in the framework, with support for advanced features like vision and function calling. The documentation and example scripts guide users through setup and usage.

Key changes in this pull request:

Zhipu AI Provider and Model Support:

  • Added ZhipuProvider class for Zhipu AI, enabling authentication via API key or environment variable, custom base URL, and HTTP client configuration. This allows users to access Zhipu's OpenAI-compatible API directly from Pydantic AI.
  • Added Zhipu AI model names (e.g., zhipu:glm-4.6, zhipu:glm-4.5, vision and code models) to the list of known models and updated model inference logic to support Zhipu as a provider. [1] [2]
  • Implemented Zhipu-specific model profile logic, handling differences from OpenAI (e.g., temperature range, strict tool definition mode, vision model support).

Documentation and Examples:

  • Added comprehensive documentation for Zhipu AI usage, including installation, configuration, available models, features (function calling, streaming, vision), and important behavioral notes.
  • Included Zhipu AI in the models overview and navigation, and added a runnable example script (examples/zhipu_example.py). [1] [2] [3]

Compatibility and Validation:

  • Improved OpenAI-compatible response handling to accommodate Zhipu's omission of the object field in API responses, ensuring schema validation succeeds.
  • Updated provider inference logic to recognize and instantiate the Zhipu provider.

Testing:

  • Added a dedicated test suite for the Zhipu provider, covering initialization, environment variable handling, error cases, and model profile features.

Fixes #2723

@DouweM DouweM self-assigned this Oct 7, 2025
* [Cohere](cohere.md)
* [Bedrock](bedrock.md)
* [Hugging Face](huggingface.md)
* [Zhipu AI](zhipu.md)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this doc to a section in models/openai.md, and move the link to the "OpenAI-compatible Providers" list below

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move a simplified version of this doc to a section in models/openai.md, along the same lines of the existing sections for other OpenAI-compatible providers. There's no need to document all the supported models (as the lists will get out of date) or things like streaming (as it works the same as with any other model, so we already have extensive docs).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example file not necessary, this can just be an example in the docs

# Some OpenAI-compatible providers (currently only Zhipu/Z.ai, see issue #2723) omit the `object` field even
# though the OpenAI schema includes it. We only patch it for that provider to avoid changing validation
# error counts in tests that purposefully feed invalid OpenAI responses (which expect 4 errors, including
# a missing `object`).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know exactly what this is referring to, but I'd rather unconditionally set response.object = 'chat.completion' here if response.object is None, and update the tests to fake invalidity with a different field. So please simplify the check and the commend, similar to the Ollama workaround we have below

# error counts in tests that purposefully feed invalid OpenAI responses (which expect 4 errors, including
# a missing `object`).
if self._provider.name == 'zhipu' and not getattr(response, 'object', None): # pragma: no branch
try: # defensive, in case attribute is read-only in future SDK versions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary, it's a Pydantic BaseModel so fields should always be writable. If that stops being the case, we can fix it when we bump the SDK.

# Ref: https://docs.bigmodel.cn/cn/guide/develop/openai/introduction
is_vision_model = model_name.startswith(('glm-4.5v', 'glm-4v')) or (
'v' in model_name and ('glm-4.5v' in model_name or 'glm-4v' in model_name)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is very redundant: 'v' in model_name and model_name.startswith('glm-4.5v') are both subsets of 'glm-4.5v' in model_name. Should we just check for the v?


# Zhipu AI models support JSON schema and object output
# All GLM-4 series models support function calling
supports_tools = model_name.startswith(('glm-4', 'codegeex-4'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name is misleading then right?


# Zhipu AI doesn't support temperature=0 (must be in range (0, 1))
# This is a known difference from OpenAI
openai_unsupported_model_settings = ()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already the default so no need to include it like this right?

return OpenAIModelProfile(
supports_json_schema_output=supports_tools,
supports_json_object_output=supports_tools,
supports_image_output=is_vision_model,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the vision models actually support image output, or just input?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Z.ai models
2 participants